package com.smartfactory.sail.controller;


import com.ada.base.common.ApiResponse;
import com.ada.base.common.ControllerResponse;
import com.ada.base.common.ResultData;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.smartfactory.sail.config.OperRecord;
import com.smartfactory.sail.entity.ItemInspectioncriteria;
import com.smartfactory.sail.service.ICommonService;
import com.smartfactory.sail.service.IItemInspectioncriteriaService;
import com.smartfactory.sail.vo.PageParam;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 检验标准 前端控制器
 * </p>
 *
 * @author 王巨壁
 * @since 2019-07-24
 */
@Api(tags = "检验标准")
@RestController
@RequestMapping("/item-inspectioncriteria")
public class ItemInspectioncriteriaController extends ControllerResponse {

    private IItemInspectioncriteriaService iItemInspectioncriteriaService;
    private ICommonService iCommonService;

    public ItemInspectioncriteriaController(IItemInspectioncriteriaService iItemInspectioncriteriaService, ICommonService iCommonService) {
        this.iItemInspectioncriteriaService = iItemInspectioncriteriaService;
        this.iCommonService = iCommonService;
    }

    @OperRecord(content = "新增检验标准",level = 1)
    @PostMapping("add")
    @ApiOperation(value = "新增检验规则", notes = "新增检验规则")
    public ApiResponse<? extends Object> add(@RequestBody ItemInspectioncriteria itemInspectioncriteria) {
        if (iItemInspectioncriteriaService.save(itemInspectioncriteria))
           return ok(itemInspectioncriteria.getId());
        return fail(ResultData.FAIL_CODE_MSG);
    }

    @OperRecord(content = "修改检验标准",level = 1)
    @PostMapping("update")
    @ApiOperation(value = "修改检验规则", notes = "修改检验规则")
    public ApiResponse<? extends Object> update(@RequestBody ItemInspectioncriteria itemInspectioncriteria) {
        if (iItemInspectioncriteriaService.updateById(itemInspectioncriteria))
            return ok();
        return fail(ResultData.FAIL_CODE_MSG);
    }

    @OperRecord(content = "删除检验标准",level = 1)
    @PostMapping("del/{id}")
    @ApiOperation(value = "删除检测规则", notes = "删除检测规则")
    public ApiResponse<? extends Object> update(@PathVariable("id") Long id) {
        if (iItemInspectioncriteriaService.removeById(id))
            return ok();
        return fail(ResultData.FAIL_CODE_MSG);
    }

    @ApiOperation(value = "检测规则列表 分页|currentPage|size|sortType|sortField 必传", notes = "检测规则列表 分页|currentPage|size|sortType|sortField 必传")
    @PostMapping("list/{currentPage}/{size}/{sortType}/{sortField}")
    public ApiResponse<Object> listMaterial(@RequestBody Map<String ,Object> queryParam,
                                            @PathVariable("currentPage")Long currentPage,
                                            @PathVariable("size")Integer size,
                                            @PathVariable("sortType")Integer sortType,
                                            @PathVariable("sortField")String sortField) {
        PageParam pageParam = new PageParam(currentPage,size,sortType,sortField);
        return  ok(iCommonService.listByPageAndQueryParam(queryParam,pageParam,"select * from item_inspectionCriteria"));
    }

    @ApiOperation(value = "根据itemId获取检测规则")
    @PostMapping("listByItemId/{itemId}")
    public ApiResponse<List<ItemInspectioncriteria>> listByItemId(@PathVariable("itemId") Long itemId) {
        QueryWrapper<ItemInspectioncriteria> qw = new QueryWrapper<>();
        qw.eq("itemId", itemId);
        return  ok(iItemInspectioncriteriaService.list(qw));
    }
}
